angular.module('functions_add', ['ui.bootstrap'])
    .controller('functions_addCtrl', ['$scope', 'httpService', 'alertmsg', '$stateParams', '$localStorage', '$rootScope', 'confirmservice', function($scope, httpService, alertmsg, $stateParams, $localStorage, $rootScope, confirmservice) {
        $scope.myInterval = 3000;
        $scope.noWrapSlides = false;
        $scope.active = 0;
        var slides = $scope.slides = [];
        var currIndex = 0;
        var index_default = 0;
        var select_addon_id;
        var id = $stateParams.id;

        function getAddons() {
            httpService.query('web/users/fetch_addons', { limit: 99999 }).then(function(res) {
                if (res.code == 200) {
                    $scope.addons = res.addons;
                    if(angular.isArray(res.addons)) {
                    	angular.forEach(res.addons, function(v, k) {
	                    	if( v.id == id ) {
	                    		index_default = k;
	                    	}
	                    });
	                    $scope.se_addon = res.addons[index_default];
                    }
                    $scope.images = $scope.se_addon.pictures;
                    select_addon_id = $scope.se_addon.id;
                }
            });
        }
        

        $scope.selectAddon = function(index) {
            $scope.se_addon = $scope.addons[index];
            $scope.images = $scope.se_addon.pictures;
            select_addon_id = $scope.se_addon.id;
        };

        $scope.addAddon = function() {
            httpService.query('web/users/add_addons', { addon_id: [select_addon_id] }, 'post').then(function(res) {
                if (res.code == 200) {
                    alertmsg('添加插件成功!');
                    window.history.go(-1);
                } else {
                    alertmsg(res.msg);
                }
            });
        };

        $scope.deleteAddon = function(addon) {
            confirmservice('是否取消应用？').then(function(bool) {
                if (bool) {
                    httpService.query('web/users/delete_addons', {addon_id: [addon.id]}, 'post').then(function(res) {
                        if (res.code == 200) {
                        	var id = $scope.addons_ids.indexOf(addon.id);
                        	$scope.addons_ids.splice(id, 1);
                            alertmsg('取消应用成功');
                        } else {
                            alertmsg('取消应用失败:' + res.msg);
                        }
                    });
                }
            });
        };

        function getPersion(id) {
            if (id) {
                httpService.query('web/users/' + id).then(function(res) {
                    if (res.code == 200) {
                        $localStorage.user = $rootScope.user = user = res.user;
                        $scope.addons = res.user.addons;
                        $scope.addons_ids = [];
                        if(angular.isArray(res.user.addons)) {
                        	angular.forEach(res.user.addons, function(v, k) {
	                            $scope.addons_ids.push(v.id);
	                        });
                        }
                        getAddons();
                    }
                });
            } else {
                $state.go('login');
            }

        };

        if ($localStorage.user) {
            getPersion($localStorage.user.id);
        }

    }]);
angular.module("ui.bootstrap", ["ui.bootstrap.tpls", "ui.bootstrap.carousel"]), angular.module("ui.bootstrap.tpls", ["uib/template/carousel/carousel.html", "uib/template/carousel/slide.html"]), angular.module("ui.bootstrap.carousel", []).controller("UibCarouselController", ["$scope", "$element", "$interval", "$timeout", "$animate", function(e, n, t, i, l) {
    function a(e) {
        for (var n = 0; n < g.length; n++) g[n].slide.active = n === e }

    function r(t, i, r) {
        if (!b) {
            if (angular.extend(t, { direction: r }), angular.extend(g[m].slide || {}, { direction: r }), l.enabled(n) && !e.$currentTransition && g[i].element && p.slides.length > 1) { g[i].element.data(h, t.direction);
                var s = p.getCurrentIndex();
                angular.isNumber(s) && g[s].element && g[s].element.data(h, t.direction), e.$currentTransition = !0, l.on("addClass", g[i].element, function(n, t) { "close" === t && (e.$currentTransition = null, l.off("addClass", n)) }) }
            e.active = t.index, m = t.index, a(i), c() } }

    function s(e) {
        for (var n = 0; n < g.length; n++)
            if (g[n].slide === e) return n }

    function o() { f && (t.cancel(f), f = null) }

    function u(n) { n.length || (e.$currentTransition = null) }

    function c() { o();
        var n = +e.interval;!isNaN(n) && n > 0 && (f = t(d, n)) }

    function d() {
        var n = +e.interval;
        v && !isNaN(n) && n > 0 && g.length ? e.next() : e.pause() }
    var f, v, p = this,
        g = p.slides = e.slides = [],
        h = "uib-slideDirection",
        m = e.active,
        b = !1;
    n.addClass("carousel"), p.addSlide = function(n, t) { g.push({ slide: n, element: t }), g.sort(function(e, n) {
            return +e.slide.index - +n.slide.index }), (n.index === e.active || 1 === g.length && !angular.isNumber(e.active)) && (e.$currentTransition && (e.$currentTransition = null), m = n.index, e.active = n.index, a(m), p.select(g[s(n)]), 1 === g.length && e.play()) }, p.getCurrentIndex = function() {
        for (var e = 0; e < g.length; e++)
            if (g[e].slide.index === m) return e }, p.next = e.next = function() {
        var n = (p.getCurrentIndex() + 1) % g.length;
        return 0 === n && e.noWrap() ? void e.pause() : p.select(g[n], "next") }, p.prev = e.prev = function() {
        var n = p.getCurrentIndex() - 1 < 0 ? g.length - 1 : p.getCurrentIndex() - 1;
        return e.noWrap() && n === g.length - 1 ? void e.pause() : p.select(g[n], "prev") }, p.removeSlide = function(n) {
        var t = s(n);
        g.splice(t, 1), g.length > 0 && m === t ? t >= g.length ? (m = g.length - 1, e.active = m, a(m), p.select(g[g.length - 1])) : (m = t, e.active = m, a(m), p.select(g[t])) : m > t && (m--, e.active = m), 0 === g.length && (m = null, e.active = null) }, p.select = e.select = function(n, t) {
        var i = s(n.slide);
        void 0 === t && (t = i > p.getCurrentIndex() ? "next" : "prev"), n.slide.index === m || e.$currentTransition || r(n.slide, i, t) }, e.indexOfSlide = function(e) {
        return +e.slide.index }, e.isActive = function(n) {
        return e.active === n.slide.index }, e.isPrevDisabled = function() {
        return 0 === e.active && e.noWrap() }, e.isNextDisabled = function() {
        return e.active === g.length - 1 && e.noWrap() }, e.pause = function() { e.noPause || (v = !1, o()) }, e.play = function() { v || (v = !0, c()) }, n.on("mouseenter", e.pause), n.on("mouseleave", e.play), e.$on("$destroy", function() { b = !0, o() }), e.$watch("noTransition", function(e) { l.enabled(n, !e) }), e.$watch("interval", c), e.$watchCollection("slides", u), e.$watch("active", function(e) {
        if (angular.isNumber(e) && m !== e) {
            for (var n = 0; n < g.length; n++)
                if (g[n].slide.index === e) { e = n;
                    break }
            var t = g[e];
            t && (a(e), p.select(g[e]), m = e) } }) }]).directive("uibCarousel", function() {
    return { transclude: !0, controller: "UibCarouselController", controllerAs: "carousel", restrict: "A", templateUrl: function(e, n) {
            return n.templateUrl || "uib/template/carousel/carousel.html" }, scope: { active: "=", interval: "=", noTransition: "=", noPause: "=", noWrap: "&" } } }).directive("uibSlide", ["$animate", function(e) {
    return { require: "^uibCarousel", restrict: "A", transclude: !0, templateUrl: function(e, n) {
            return n.templateUrl || "uib/template/carousel/slide.html" }, scope: { actual: "=?", index: "=?" }, link: function(n, t, i, l) { t.addClass("item"), l.addSlide(n, t), n.$on("$destroy", function() { l.removeSlide(n) }), n.$watch("active", function(n) { e[n ? "addClass" : "removeClass"](t, "active") }) } } }]).animation(".item", ["$animateCss", function(e) {
    function n(e, n, t) { e.removeClass(n), t && t() }
    var t = "uib-slideDirection";
    return { beforeAddClass: function(i, l, a) {
            if ("active" === l) {
                var r = !1,
                    s = i.data(t),
                    o = "next" === s ? "left" : "right",
                    u = n.bind(this, i, o + " " + s, a);
                return i.addClass(s), e(i, { addClass: o }).start().done(u),
                    function() { r = !0 } }
            a() }, beforeRemoveClass: function(i, l, a) {
            if ("active" === l) {
                var r = !1,
                    s = i.data(t),
                    o = "next" === s ? "left" : "right",
                    u = n.bind(this, i, o, a);
                return e(i, { addClass: o }).start().done(u),
                    function() { r = !0 } }
            a() } } }]), angular.module("uib/template/carousel/carousel.html", []).run(["$templateCache", function(e) { e.put("uib/template/carousel/carousel.html", '<div class="carousel-inner" ng-transclude></div>\n<a role="button" href class="left carousel-control" ng-click="prev()" ng-class="{ disabled: isPrevDisabled() }" ng-show="slides.length > 1">\n  <span aria-hidden="true" class="glyphicon glyphicon-chevron-left"></span>\n  <span class="sr-only">previous</span>\n</a>\n<a role="button" href class="right carousel-control" ng-click="next()" ng-class="{ disabled: isNextDisabled() }" ng-show="slides.length > 1">\n  <span aria-hidden="true" class="glyphicon glyphicon-chevron-right"></span>\n  <span class="sr-only">next</span>\n</a>\n<ol class="carousel-indicators" ng-show="slides.length > 1">\n  <li ng-repeat="slide in slides | orderBy:indexOfSlide track by $index" ng-class="{ active: isActive(slide) }" ng-click="select(slide)">\n    <span class="sr-only">slide {{ $index + 1 }} of {{ slides.length }}<span ng-if="isActive(slide)">, currently active</span></span>\n  </li>\n</ol>\n') }]), angular.module("uib/template/carousel/slide.html", []).run(["$templateCache", function(e) { e.put("uib/template/carousel/slide.html", '<div class="text-center" ng-transclude></div>\n') }]), angular.module("ui.bootstrap.carousel").run(function() {!angular.$$csp().noInlineStyle && !angular.$$uibCarouselCss && angular.element(document).find("head").prepend('<style type="text/css">.ng-animate.item:not(.left):not(.right){-webkit-transition:0s ease-in-out left;transition:0s ease-in-out left}</style>'), angular.$$uibCarouselCss = !0 });
